package com.innoking.docsys.workflow.service.impl;

import com.innoking.docsys.workflow.dao.cluster.ClusterUpmsActDefineDao;
import com.innoking.docsys.workflow.dao.cluster.ClusterUpmsActDefineExtDao;
import com.innoking.docsys.workflow.dto.UpmsActDefine;
import com.innoking.docsys.workflow.example.UpmsActDefineClusterExample;
import com.innoking.docsys.workflow.service.WorkflowDefineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述:
 *
 * @author zyp
 * @description 工作流定义业务层实现类
 * @create 2019-02-26 13:44
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class WorkflowDefineServiceImpl implements WorkflowDefineService {


    private static final Logger logger = LoggerFactory.getLogger(WorkflowDefineServiceImpl.class);

    @Autowired
    private ClusterUpmsActDefineDao clusterUpmsActDefineDao;

    @Autowired
    private ClusterUpmsActDefineExtDao clusterUpmsActDefineExtDao;

    @Override
    public int insert(UpmsActDefine upmsActDefine) {
        int count = clusterUpmsActDefineDao.insert(upmsActDefine);
        return count;
    }

    @Override
    public List<UpmsActDefine> selectDefineByKey(String key) {
        UpmsActDefineClusterExample upmsActDefineClusterExample = new UpmsActDefineClusterExample();
        UpmsActDefineClusterExample.Criteria criteria = upmsActDefineClusterExample.createCriteria();
        criteria.andAdKeyEqualTo(key);
        criteria.andAdStatusEqualTo(1L);
        List<UpmsActDefine> upmsActDefines = clusterUpmsActDefineDao.selectByExample(upmsActDefineClusterExample);
        return upmsActDefines;
    }

    /**
     * @author:zyp
     * @description 根据流程关联修改当前使用状态
     * @date: 2019/2/26 14:21
     * @return 
     */
    @Override
    public int updateStatusByAdCode(String adCode) {
        int count = clusterUpmsActDefineExtDao.updateStatusByAdCode(adCode);
        return count;
    }

    /**
     * @author:zyp
     * @description 查看是否存在，根据key与code
     * @date: 2019/9/9 15:47
     * @return 
     */
    @Override
    public boolean isExistByCodeKey(String key, String adCode) {
        UpmsActDefineClusterExample upmsActDefineClusterExample = new UpmsActDefineClusterExample();
        UpmsActDefineClusterExample.Criteria criteria = upmsActDefineClusterExample.createCriteria();
        criteria.andAdKeyEqualTo(key);
        criteria.andAdCodeEqualTo(adCode);
        List<UpmsActDefine> upmsActDefines = clusterUpmsActDefineDao.selectByExample(upmsActDefineClusterExample);
        if(upmsActDefines!=null&&upmsActDefines.size()>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @author:zyp
     * @description 更新工作流定义
     * @date: 2019/9/9 15:59
     * @return 
     */
    @Override
    public void updateWorkflowDefine(UpmsActDefine upmsActDefine, UpmsActDefineClusterExample upmsActDefineExample) {
        clusterUpmsActDefineDao.updateByExampleSelective(upmsActDefine,upmsActDefineExample);
    }

    /**
     * @author:zyp
     * @description 根据工作流code查看数据
     * @date: 2019/9/10 10:36
     * @return
     */
    @Override
    public UpmsActDefine getNewAct(String activitiType) {
        UpmsActDefineClusterExample upmsActDefineClusterExample = new UpmsActDefineClusterExample();
        UpmsActDefineClusterExample.Criteria criteria =  upmsActDefineClusterExample.createCriteria();
        criteria.andAdCodeEqualTo(activitiType);
        criteria.andAdStatusEqualTo(1L);
        List<UpmsActDefine> upmsActDefines = clusterUpmsActDefineDao.selectByExample(upmsActDefineClusterExample);
        if(upmsActDefines==null||upmsActDefines.size()==0){
            return null;
        }
        return upmsActDefines.get(0);
    }

}